package com.geeklan.base.base;

import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.lifecycle.ViewModel;
import androidx.viewbinding.ViewBinding;

import com.geeklan.base.ble.BluetoothLeManager;
import com.geeklan.base.network.NetworkMonitor;
import com.geeklan.base.utils.ActivityUtil;
import com.geeklan.base.widget.MyProgressDialog;
import com.smart.base.R;

import org.greenrobot.eventbus.EventBus;

public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity implements NetworkMonitor.NetworkCallBack {
    protected T mBinding;
    protected abstract T initBinding();
    protected abstract void initData();
    protected abstract void initView();
    protected abstract void onApplyWindowInsets();
    public boolean isRegisterEvent(){
        return false;
    }
    public boolean isRegisterNetwork(){
        return false;
    }

    public void onPermissionsResultSuccess(){

    }
    public void onPermissionsResultFail(){

    }
    private MyProgressDialog progressDialog;
    private NetworkMonitor mNetworkMonitor;
    public static final int PERMISSION_REQ_ID = 22;
    public static final int REQUEST_CODE_CHOOSE = 100;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);

        EdgeToEdge.enable(this);
        mBinding = initBinding();
        setContentView(mBinding.getRoot());
        onApplyWindowInsets();
        ActivityUtil.getInstance().addActivity(this);
        progressDialog = new MyProgressDialog(this);
        if(isRegisterEvent()){
            EventBus.getDefault().register(this);
        }
        if(isRegisterNetwork()){
            mNetworkMonitor = new NetworkMonitor(this,this);
            mNetworkMonitor.startNetworkMonitoring();
        }

        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityUtil.getInstance().removeActivity(this);
        if(isRegisterEvent()){
            EventBus.getDefault().unregister(this);
        }
        if(isRegisterNetwork()) {
            if (mNetworkMonitor != null) {
                mNetworkMonitor.stopNetworkMonitoring();
            }
        }
    }

    public void finishAll() {
        ActivityUtil.getInstance().finishAllActivity();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQ_ID) {
            if (grantResults.length > 0 && allPermissionsGranted(grantResults)) {
                onPermissionsResultSuccess();
            } else {
                onPermissionsResultFail();
            }
        }
    }


    private boolean allPermissionsGranted(int[] grantResults) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public void loading(){
        progressDialog.show();
    }

    public boolean isLoading(){
        return progressDialog.isShowing();
    }

    public void loading(String text){
        progressDialog.setLoadingTest(text);
        progressDialog.show();
    }

    public void loading(int text){
        progressDialog.setLoadingTest(text);
        progressDialog.show();
    }

    public void dismiss(){
        if(isLoading()) {
            progressDialog.dismiss();
        }
    }
}
